// topic sort service
var app = angular.module('app');

app.factory('treeUtilsService', ['$q', function ($q) {
    var treeUtilsService = {};


    treeUtilsService.formatData = function (root, des) {
        // attribute
        des = des || {};
        des.data = des.data || {};
        if (Object.prototype.toString.call(root.memberCount) !== '[object Undefined]') {
            des.label = root.name + '(' + root.memberCount + ')';
        } else {
            des.label = root.name + '(0)';
        }
        des.data.description = root.description;
        des.data.users = root.users;

        if (root.children && root.children.length != 0) {
            des.children = new Array(root.children.length);
            for (var i = 0; i < root.children.length; i++) {
                treeUtilsService.formatData(root.children[i], des.children[i]);
            }
        } else {
            des.children = [];
        }

        return des;
    };

    treeUtilsService.formatContactGroupTree = function (root) {
        // attribute
        var des = {};

        formatContactGroupTreeRecursive(root, des);

        return des;
    };

    function formatContactGroupTreeRecursive(root, des) {
        // attribute
        if (!des) {
            return;
        }
        des.data = des.data || {};
        if (Object.prototype.toString.call(root.memberCount) !== '[object Undefined]') {
            des.label = root.name + '(' + root.memberCount + ')';
        } else {
            des.label = root.name + '(0)';
        }
        des.data.id = root._id;
        des.noLeaf = true;

        if (root.children && root.children.length != 0) {
            des.children = [];
            for (var i = 0; i < root.children.length; i++) {
                des.children.push({});
            }
            for (var i = 0; i < root.children.length; i++) {
                formatContactGroupTreeRecursive(root.children[i], des.children[i]);
            }
        } else {
            des.children = [];
        }

        return des;
    }

    /**
     * get groups by ids
     */
    treeUtilsService.getGroupsByIds = function (root, groupIds) {
        if (!root || !groupIds || groupIds.length === 0) {
            return;
        }
        var results = [];
        if (groupIds.indexOf(root.data.id) !== -1) {
            results.push(root);
        }
        if (root.children && root.children.length !== 0) {
            for (var i = 0; i < root.children.length; i++) {
                getGroupsByIdsRecursive(root.children[i], groupIds, results);
            }
        }

        return results;
    };

    function getGroupsByIdsRecursive(node, groupIds, results) {

        if (groupIds.indexOf(node.data.id) != -1) {
            results.push(node);
        }

        if (node.children && node.children.length != 0) {
            for (var i = 0; i < node.children.length; i++) {
                getGroupsByIdsRecursive(node.children[i], groupIds, results);
            }
        }

        return results;
    }

    return treeUtilsService;
}]);